using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-产品报废单据表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpScrapService : IApplicationService 
{
    private readonly SugarRepository<ErpScrap> _thisRepository;
    private readonly SugarRepository<ErpScrapProduct> _scrapProductRepository;
    public ErpScrapService(SugarRepository<ErpScrap> thisRepository
    ,SugarRepository<ErpScrapProduct> scrapProductRepository)
    {
        _thisRepository = thisRepository;
        _scrapProductRepository = scrapProductRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpScrapDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Title.Contains(param.Key))
            .Includes(m=>m.Employee)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ErpScrapDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpScrapDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m=>m.Employee)
            .FirstAsync(m=>m.Id==id);
        return model.Adapt<ErpScrapDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task AddAsync(ErpScrapDto model)
    {
        var scrapId=await _thisRepository.InsertReturnSnowflakeIdAsync(model.Adapt<ErpScrap>());
        var addScrapProduct = new List<ErpScrapProductDto>();
        foreach (var item in model.ScrapProduct)
        {
            addScrapProduct.Add(new ErpScrapProductDto()
            {
                ScrapId = scrapId,
                ProductId = item.ProductId,
                Count = item.Count
            });
        }
        await _scrapProductRepository.InsertRangeAsync(addScrapProduct.Adapt<List<ErpScrapProduct>>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task ModifyAsync(ErpScrapDto model)
    {
        await _thisRepository.UpdateAsync(model.Adapt<ErpScrap>());
        
        await _scrapProductRepository.DeleteAsync(m => m.ScrapId==model.Id);
        var addScrapProduct = new List<ErpScrapProductDto>();
        foreach (var item in model.ScrapProduct)
        {
            addScrapProduct.Add(new ErpScrapProductDto()
            {
                ScrapId = model.Id,
                ProductId = item.ProductId,
                Count = item.Count
            });
        }
        await _scrapProductRepository.InsertRangeAsync(addScrapProduct.Adapt<List<ErpScrapProduct>>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete,UnitOfWork]
    public async Task DeleteAsync([FromBody] List<long> ids)
    {
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
        await _scrapProductRepository.DeleteAsync(m => ids.Contains(m.ScrapId));
    }
}
